package com.mvcplus.common.controller;

import com.mvcplus.common.utils.LoggerUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2016/10/27.
 */
@Controller
@Scope(value="prototype")
public class CommonController extends BaseController {


    /*
    * 欢迎页跳转
    * @return
    * */

    @RequestMapping("/")
    public String welcome(){
        return "redirect:/main/index";
    }

    /**
     * 404错误
     * @param request
     * @return
     */
    @RequestMapping("common/404")
    public ModelAndView _404(HttpServletRequest request){
        ModelAndView view = new ModelAndView("common/404");
        return view;
    }
    /**
     * 500错误
     * @param request
     * @return
     */
    @RequestMapping("common/500")
    public ModelAndView _500(HttpServletRequest request){
        ModelAndView view = new ModelAndView("common/500");

        Throwable t = (Throwable)request.getAttribute("javax.servlet.error.exception");
        String defaultMessage = "未知" ;
        if(null == t){
            view.addObject("line", defaultMessage);
            view.addObject("clazz", defaultMessage);
            view.addObject("methodName",defaultMessage);
            return view;
        }
        String message = t.getMessage() ;//错误信息
        StackTraceElement[] stack = t.getStackTrace();
        view.addObject("message", message);
        if(null != stack && stack.length != 0 ){
            StackTraceElement element = stack[0];
            int line = element.getLineNumber();//错误行号
            String clazz = element.getClassName();//错误java类
            String fileName = element.getFileName();

            String methodName = element.getMethodName() ;//错误方法
            view.addObject("line", line);
            view.addObject("clazz", clazz);
            view.addObject("methodName",methodName);
            LoggerUtils.fmtError(getClass(), "line:%s,clazz:%s,fileName:%s,methodName:%s()",
                    line,clazz,fileName,methodName);
        }
        return view;
    }

    /**
     * noSession
     * @param request
     * @return
     */
    @RequestMapping("common/noSession")
    public ModelAndView noSession(HttpServletRequest request){
        ModelAndView view = new ModelAndView("common/noSession");
        return view;
    }

    /**
     * noSecurity
     * @param request
     * @return
     */
    @RequestMapping("common/noSecurity")
    public ModelAndView noSecurity(HttpServletRequest request){
        ModelAndView view = new ModelAndView("common/noSecurity");
        return view;
    }



}
